﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;

namespace YJKExcelFormTool.Forms
{
	public partial class ViewSelectionForm : System.Windows.Forms.Form
	{
		public bool IsNewName
		{
			get
			{
				return this.rbtnNewView.Checked;
			}
		}

		public string NewName
		{
			get
			{
				return this.tbNewName.Text;
			}
		}

		public int SelectViewIndex
		{
			get
			{
				return this.cmbViewNameList.SelectedIndex;
			}
		}

		public ViewSelectionForm(string newName, string prefix, List<string> nameList, ExternalCommandData cmdData)
		{
			this.InitializeComponent();
			this.m_Doc = cmdData.Application.ActiveUIDocument.Document;
			this.m_NewName = newName;
			this.rbtnNewView.Checked = true;
			this.tbNewName.Text = this.m_NewName;
			this.m_Prefix = prefix;
			this.rbtnSelectView.Checked = false;
			this.m_NameList.AddRange(nameList);
			foreach (string item in this.m_NameList)
			{
				this.cmbViewNameList.Items.Add(item);
			}
			if (nameList.Count == 0)
			{
				this.rbtnNewView.Enabled = false;
				this.rbtnSelectView.Enabled = false;
				this.cmbViewNameList.Enabled = false;
				return;
			}
			this.rbtnNewView.Enabled = true;
			this.rbtnSelectView.Enabled = true;
			this.cmbViewNameList.Enabled = true;
			this.cmbViewNameList.SelectedIndex = 0;
		}

		public ViewSelectionForm(string newName, string prefix, List<string> nameList, bool isChangeIcon = false)
		{
			this.InitializeComponent();
			if (isChangeIcon)
			{
                //base.Icon = Resource.LonghuIcon;
			}
			this.m_NewName = newName;
			this.rbtnNewView.Checked = true;
			this.tbNewName.Text = this.m_NewName;
			this.m_Prefix = prefix;
			this.rbtnSelectView.Checked = false;
			this.m_NameList.AddRange(nameList);
			foreach (string item in this.m_NameList)
			{
				this.cmbViewNameList.Items.Add(item);
			}
			if (nameList.Count == 0)
			{
				this.rbtnNewView.Enabled = false;
				this.rbtnSelectView.Enabled = false;
				this.cmbViewNameList.Enabled = false;
				return;
			}
			this.rbtnNewView.Enabled = true;
			this.rbtnSelectView.Enabled = true;
			this.cmbViewNameList.Enabled = true;
			this.cmbViewNameList.SelectedIndex = 0;
		}

		private void ViewSelectionForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (base.DialogResult == DialogResult.OK)
			{
				string newName = this.tbNewName.Text;
				if (newName.CompareTo("") == 0)
				{
					YJKMessageBox.Show("视图名称不能为空！");
					e.Cancel = true;
					return;
				}
				if (newName.IndexOf(this.m_Prefix) == -1)
				{
					YJKMessageBox.Show("视图名称必须包含\"" + this.m_Prefix + "\"！");
					e.Cancel = true;
					return;
				}
				if (this.m_NameList.FindIndex((string s) => s.CompareTo(newName) == 0) != -1)
				{
					YJKMessageBox.Show("该视图名称已经存在，请重新命名！");
					e.Cancel = true;
					return;
				}
				this.SaveData();
			}
		}

		private void ViewSelectionForm_Load(object sender, EventArgs e)
		{
			this.ReadData();
		}

		private void ReadData()
		{
			string path = Path.GetTempPath() + "ViewSelectionForm.dat";
			if (!File.Exists(path))
			{
				this.rbtnNewView.Checked = true;
				this.rbtnSelectView.Checked = false;
				this.ChangeContrlEnabled();
				return;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			try
			{
				this.rbtnNewView.Checked = Convert.ToBoolean(streamReader.ReadLine());
				this.rbtnSelectView.Checked = Convert.ToBoolean(streamReader.ReadLine());
				int num = Convert.ToInt32(streamReader.ReadLine());
				if (num == -1 || num >= this.m_NameList.Count)
				{
					this.rbtnNewView.Checked = true;
					this.rbtnSelectView.Checked = false;
					streamReader.Close();
					fileStream.Close();
					this.ChangeContrlEnabled();
				}
				else
				{
					this.ChangeContrlEnabled();
					this.cmbViewNameList.SelectedIndex = num;
					streamReader.Close();
					fileStream.Close();
				}
			}
			catch
			{
				this.rbtnNewView.Checked = true;
				this.rbtnSelectView.Checked = false;
				this.ChangeContrlEnabled();
				streamReader.Close();
				fileStream.Close();
			}
		}

		private void SaveData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "ViewSelectionForm.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(this.rbtnNewView.Checked);
			streamWriter.WriteLine(this.rbtnSelectView.Checked);
			streamWriter.WriteLine(this.cmbViewNameList.SelectedIndex);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private void rbtnNewView_CheckedChanged(object sender, EventArgs e)
		{
			this.ChangeContrlEnabled();
		}

		private void ChangeContrlEnabled()
		{
			if (this.rbtnNewView.Checked)
			{
				this.tbNewName.Enabled = true;
				this.cmbViewNameList.Enabled = false;
				return;
			}
			this.tbNewName.Enabled = false;
			this.cmbViewNameList.Enabled = true;
		}

		protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
		{
			int num = 256;
			int num2 = 260;
			if ((msg.Msg == num | msg.Msg == num2) && keyData == Keys.Escape)
			{
				base.Close();
			}
			return false;
		}

		private void btnCurrent_Click(object sender, EventArgs e)
		{
			if (this.m_Doc.ActiveView is View3D)
			{
				YJKMessageBox.Show("不能在三维视图中绘制图例，请选择其他选项！");
				return;
			}
			base.DialogResult = DialogResult.Abort;
		}

		private string m_NewName = "";

		private string m_Prefix = "";

		private List<string> m_NameList = new List<string>();

		private Document m_Doc;
	}
}
